package com.yanjiali.webmagic;

import com.yanjiali.constant.ThirdPartyCoverConstant;
import com.yanjiali.entity.ThirdParty;
import com.yanjiali.service.ThirdPartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.yanjiali.webmagic.demo
 * @Author: yanjiali
 * @Created: 2025/3/11 16:52
 * TODO 处理爬虫获取的数据
 */
@Component
public class BingPipeline implements Pipeline {

    @Autowired
    private ThirdPartyService thirdPartyService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        List<ThirdParty> thirdPartyList = new ArrayList<>();
        List<String> dataList = resultItems.get("dataList");

        for (String item : dataList) {
            Html html = new Html(item);
            String title = html.xpath("//h2/a/text()").get();
            String summarize = html.xpath("//p/text()").get();
            String url = html.css("a.tilk").links().toString();

            //TODO 封装数据
            ThirdParty thirdParty = new ThirdParty();
            thirdParty.setTitle(title);
            thirdParty.setIntroduce(summarize);
            thirdParty.setUrl(url);
            thirdParty.setCover(ThirdPartyCoverConstant.coverUrl);
            thirdPartyList.add(thirdParty);
        }

        //入库
        if(thirdPartyList != null && thirdPartyList.size() > 0) {
            thirdPartyService.saveThirdPartyList(thirdPartyList);
        }
    }
}
